
import cloud from '@lafjs/cloud'
import nw from "nw-lafjs";
import {fail, ok} from "@/utils/responseUtil";
const _ = cloud.database().command
export default async function (ctx: FunctionContext) {
  let body = ctx.body
  /**
   * {
   *     "name": "112",
   *     "sort": "1212",
   *     "remark": "1212"
   * }
   */
  if (nw.util.isNull(body.name)){
    return fail("平台属性分组名称不能为空")
  }
  if (nw.util.isNull(body.sort)){
    body.sort = 1
  }
  if (nw.util.isNull(body.remark)){
    body.remark = ""
  }
  //校验分组名称是否重复
  let count = await nw.db.count({
    dbName:"attrGroup",
    whereJson:{deleted:0,name:_.eq(body.name)}
  })
  if (count>0){
    return fail("分组名称重复")
  }
  //构建保存结果
  let data = {
    name:body.name,
    sort:body.sort,
    remark:body.remark,
    createTime:new Date(),
    updateTime:new Date(),
    deleted:0
  }

  let id = await nw.db.add({
    dbName:"attrGroup",
    dataJson:data
  })
  if (nw.util.isNull(id)){
    return fail("保存失败")
  }
  return ok("保存成功")

}
